﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraGrid;


using MINERP.BLL.CustomerManage;

using DevExpress.XtraGrid.Views.Grid;
using System.Windows.Forms;
using System.Drawing;
using MINERP.COM;


namespace MINERP.CustomerManage.Controller
{
    public class WorkOrderFlowListController : ControllerBase
    {
        private GridControl                         _grid = null;
        private WorkOrderFlowListManageBLL          _bll = new WorkOrderFlowListManageBLL();
        private OrderBagManageBLL                   _orderBll = new OrderBagManageBLL();
        private GridView                            _view = null;

        private int                                 _viewCount = 0;

        private List<WorkOrderFlowListEntity>       _resultList = null;

        internal GridControl Grid
        {
            set
            {
                this._grid = value;
            }
        }

        internal GridView View
        {
            set
            {
                this._view = value;
            }
        }

        /// <summary>
        /// 显示流程节点信息
        /// </summary>
        /// <param name="infoID"></param>
        internal void GetWorkOrderFlowListByOrderInfo(int infoID)
        {
            _resultList = _bll.GetWorkerFlowListByOrderInfo(infoID);

            this._grid.DataSource = _resultList;
            this._grid.RefreshDataSource();
        }
        /// <summary>
        /// 显示流程节点信息(备份)
        /// </summary>
        /// <param name="infoID"></param>
        internal void GetWorkOrderFlowListByOrderInfo_bk(int infoID)
        {
            _resultList = _bll.GetWorkerFlowListByOrderInfo_bk(infoID);
            if (_resultList.Count == 0)
            {
                _resultList = _bll.GetWorkerFlowListByOrderInfo(infoID);
            }

            this._grid.DataSource = _resultList;
            this._grid.RefreshDataSource();
        }

        internal List<WorkOrderFlowListEntity> GetWorkOrderFlowListByOrderInfoID(int infoID)
        {
            return _bll.GetWorkerFlowListByOrderInfo(infoID);
        }

        /// <summary>
        /// 得到产品的生产状态
        /// </summary>
        /// <param name="infoid"></param>
        /// <returns></returns>
        internal bool GetOrderInfoStatus(int infoid)
        {
            return _bll.GetOrderInfoStats(infoid);
        }

        /// <summary>
        /// 把正在生产的节点停止
        /// </summary>
        /// <param name="infoid"></param>
        internal void SetUnProductStatus(int infoid)
        {
            List<WorkOrderFlowListEntity> list = _bll.GetWorkerFlowListByOrderInfo(infoid);

            foreach (var item in list)
            {
                if (item.StatusValue != (int)OrderFlowListStatus.Complete)
                {
                    //停止节点
                    item.StatusValue = (int)OrderFlowListStatus.UnProduct;
                    break;
                }
            }

            //停止数据库中的状态
            int status = (int)COM.OrderInfoStatus.Stop;
            _orderBll.UpdateProductStatus(infoid, status);
            //_bll.Update();
        }

        /// <summary>
        /// 恢复停止生产的节点
        /// </summary>
        /// <param name="infoid"></param>
        internal void SetProductStatus(int infoid)
        {
            List<WorkOrderFlowListEntity> list = _bll.GetWorkerFlowListByOrderInfo(infoid);

            foreach (var item in list)
            {
                if (item.StatusValue != (int)OrderFlowListStatus.Complete)
                {
                    //恢复节点
                    item.StatusValue = (int)OrderFlowListStatus.Useable;
                    break;
                }
            }

            //恢复产品的生产状态
            int status = (int)COM.OrderInfoStatus.Doing;
            _orderBll.UpdateProductStatus(infoid, status);
            //_bll.Update();
        }


        /// <summary>
        /// 得到所有的用户信息
        /// </summary>
        /// <returns></returns>
        internal List<UserInfo> GetAllUser()
        {
            return _bll.GetUserInfo();
        }
        /// <summary>
        /// 得到所有的节点信息
        /// </summary>
        /// <returns></returns>
        internal void GetAllTechName()
        {
            _bll.GetAllTechName();
        }

        /// <summary>
        /// 得到产品下的所有节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        internal void GetFlowList(object sender, RowClickEventArgs e,int status)                //status表示该产品所在的订单是否已完成
        {
            
            GridView view = sender as GridView;
            
            OrderInfoEntity obj = (OrderInfoEntity)view.GetRow(e.RowHandle);

            if (obj != null)
            {
                if (status == (int)COM.OrderBagStatus.Completed)
                {
                    this.GetWorkOrderFlowListByOrderInfo_bk(obj.OrderProductId);
                }
                else
                {
                    this.GetWorkOrderFlowListByOrderInfo(obj.OrderProductId);
                }
            }
            else
            {
                this._grid.DataSource = null;
                this._grid.RefreshDataSource();
            }
        }

        /// <summary>
        /// 设置指定产品的流程
        /// </summary>
        /// <param name="orderInfo"></param>
        public void SetCurrentFlowList(OrderInfoEntity orderInfo,int orderStatus)
        {
            if (orderInfo != null)
            {
                if (orderStatus == (int)COM.OrderBagStatus.Completed)
                {
                    this.GetWorkOrderFlowListByOrderInfo_bk(orderInfo.OrderProductId);
                }
                else
                {
                    this.GetWorkOrderFlowListByOrderInfo(orderInfo.OrderProductId);
                }
            }
            else
            {
                this._grid.DataSource = null;
                this._grid.RefreshDataSource();
            }
        }

        /// <summary>
        /// 清空流程
        /// </summary>
        internal void SetEmptyFlow()
        {
            this._grid.DataSource = null;
            this._grid.RefreshDataSource();
        }


        /// <summary>
        /// 显示报警色
        /// </summary>
        /// <param name="e"></param>
        internal void CheckAlarm(RowStyleEventArgs e)
        {
            WorkOrderFlowListEntity item = this._view.GetRow(e.RowHandle) as WorkOrderFlowListEntity;

            if (item != null)
            {
                if (item.StatusValue == (int)OrderFlowListStatus.Complete)
                {
                    return;
                }
                else
                {
                    //如果节点还未完成检查节点是否超时
                    if (item.PreCompleteTime != null)
                    {
                        DateTime tempPreTime = (DateTime)item.PreCompleteTime;
                        if (tempPreTime <= DateTime.Now.AddHours(Properties.Settings.Default.AlarmTime))
                        {
                            e.Appearance.BackColor = Color.LightPink;
                            e.Appearance.BackColor2 = Color.LightPink;
                        }

                        if (item.StatusValue == (int)OrderFlowListStatus.UnProduct)
                        {
                            e.Appearance.BackColor = Color.LightSlateGray;
                            e.Appearance.BackColor2 = Color.LightSlateGray;
                        }
                    }

                }

            }
        }

        /// <summary>
        /// 把记录前移
        /// </summary>
        internal void PreRecord()
        {
            WorkOrderFlowListEntity focusObj = (WorkOrderFlowListEntity)this._view.GetFocusedRow();

            if (focusObj == null)
            {
                MessageBox.Show("没有选中任何行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            int index = _resultList.IndexOf(focusObj);

            if (index < 1)
            {
                MessageBox.Show("已经是第一条记录，不能前移", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            WorkOrderFlowListEntity preObj = _resultList.ElementAt(index - 1);

            //改变工序的值
            int tempOrder = focusObj.WorkOrder;
            focusObj.WorkOrder = preObj.WorkOrder;
            preObj.WorkOrder = tempOrder;

            //交换位置
            _resultList.Sort(CompareObj);

            this._view.RefreshData();
            this._view.FocusedRowHandle = index-1;

            _bll.Update();
        }

        /// <summary>
        /// 把记录后移
        /// </summary>
        internal void NextRecord()
        {
            _viewCount = this._view.RowCount;

            WorkOrderFlowListEntity focusObj = (WorkOrderFlowListEntity)this._view.GetFocusedRow();

            if (focusObj == null)
            {
                MessageBox.Show("没有选中任何行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            int index = _resultList.IndexOf(focusObj);


            if (index == _viewCount - 1)
            {
                MessageBox.Show("已经是最后一条记录，不能后移", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            WorkOrderFlowListEntity nextObj = _resultList.ElementAt(index + 1);

            //改变工序的值
            int tempOrder = focusObj.WorkOrder;
            focusObj.WorkOrder = nextObj.WorkOrder;
            nextObj.WorkOrder = tempOrder;

            //交换位置
            _resultList.Sort(CompareObj);

            this._view.RefreshData();
            this._view.FocusedRowHandle = index + 1;

            _bll.Update();
        }

        #region //helper methods


        private int CompareObj(WorkOrderFlowListEntity objA, WorkOrderFlowListEntity objB)
        {
            return objA.WorkOrder - objB.WorkOrder;
        }

        #endregion
    }
}
